<template>
  <div>
    <navigation-bar>
      <div class="searchContent">
        <el-input v-model="examineeName" placeholder="请输入名称" size="mini"
                  style="width: 150px; margin-right: 10px"></el-input>
        <el-select
            v-model="specialCheckItem"
            clearable
            size="mini"
            style="width: 120px; margin-right: 10px"
            placeholder="请选择检查项"
        >
          <el-option
              :value="item.value"
              :label="item.label"
              v-for="(item, index) in specialCheckItemList"
              v-bind:key="index"
          ></el-option>
        </el-select>
        <custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
        <custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
        <custom-button type="add" style="margin-right: 10px" @addClick="addClick"></custom-button>
      </div>
    </navigation-bar>
    <el-row>
      <el-col :span="5">
        <div style="width: 100%">
          <user-list @handleSelectionChange="handleSelectionChange" stage="filter"
                     businessType="specialCheck"  ref="userList"  @addClick="addClick"></user-list>
        </div>
      </el-col>
      <el-col :span="19">
        <table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" :pageSize="pageSize"
                     :pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @editRow="editRow"
                     @viewRow="viewRow"
                     @deleteRow="deleteRow" @lookSMJRow="lookSMJRow" btnListWidth="200px"></table-index>
      </el-col>
    </el-row>
    <div v-if="dialogShow">
      <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
        <form-index :formConfig="formConfig" @submit="submit" :type="type">
        </form-index>
      </custom-dialog>
    </div>
    <div v-if="lmjDialogShow">
      <custom-dialog title="查看扫描件" size="small" @dialogClose="lmjDialogClose">
        <look-scan :ImgBase64List="fileList"></look-scan>
      </custom-dialog>
    </div>
    <div v-if="smjDialogShow">
      <custom-dialog title="上传扫描件" size="small" @dialogClose="smjDialogClose">
        <upload-file :limit="6" :multiple="true" @uploadFileHandle="uploadFileHandle"></upload-file>
        <div style="margin-top: 10px; text: right; text-align: right; height: 40px">
          <el-button type="primary" size="mini" @click="fileSure">确定</el-button>
        </div>
      </custom-dialog>
    </div>
  </div>
</template>

<script>
import {specialScreeningForm} from "@/assets/form/managementCenter/subjectManagement/filteringPeriodManagement/specialScreeningForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import lookScan from "@/components/customComponents/managementCenter/lookScan"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import uploadFile from "@/components/customComponents/managementCenter/uploadFile"
import userList from "@/components/customComponents/managementCenter/userList"
import {AjaxCommon} from "@/libs/apiCommon"
import {PopupCommon} from "@/libs/popupCommon"
import {SessionCommon} from "@/libs/sessionCommon"
import {Utility} from "@/libs/utility"

export default {
  components: {
    navigationBar,
    customButton,
    tableIndex,
    customDialog,
    formIndex,
    userList,
    lookScan,
    uploadFile,
  },
  data() {
    return {
      specialCheckItem: "",
      specialCheckItemList: Utility.getDictList("Inspection_items"),
      examineeCodeList: [],
      fileLists: "",
      smjDialogShow: false,
      lmjDialogShow: false,
      leftSelectIds: [],
      leftDataSource: [],
      leftTableColumns: [
        {
          prop: "examineeName",
          label: "受试者姓名",
        },
      ],
      examineeName: "",
      // table 相关参数  begin
      dataSource: [],
      tableColumns: [
        {
          prop: "filterNo",
          label: "筛选号",
          width: 110,
        },
        {
          prop: "examineeName",
          label: "受试者姓名",
          width: 120,
        },
        {
          prop: "sex",
          label: "性别",
          dictName: "sex",
          width: 100,
        },
        {
          prop: "specialCheckItem",
          label: "检查项",
          width: 120,
          dictName: "Inspection_items",
        },
        {
          prop: "checkBeginTime",
          label: "检查开始时间",
          width: 160,
        },
        {
          prop: "checkEndTime",
          label: "检查结束时间",
          width: 160,
        },
        {
          prop: "checkJudge",
          label: "检查结果",
          width: 120,
          dictName: "check_judge"
        },
        {
          prop: "createByName",
          label: "检查人",
          width: 120
        },
        {
          prop: "verifyByName",
          label: "核对人",
          width: 120,
        },
        {
          prop: "verifyTime",
          label: "核对时间",
          width: 160,
        },
        {
            prop: "remarks",
            label: "备注",
            width: 300
        },
      ],
      btnList: ["edit", "view", "lookSMJ", "delete"],
      pageSize: 20,
      pageCount: 100,
      pageNo: 1,
      // table 相关参数  end

      // 弹窗是否显示
      dialogShow: false,
      // 弹窗标题 (新增修改不同)
      dialogTitle: "",

      // form的配置信息
      formConfig: specialScreeningForm,
      type: "add",
      fileList: [],
    }
  },
  methods: {
    fileSure() {
      if (this.fileLists == "") {
        PopupCommon.notify("info", "请至少选择一个文件")
        return
      }
      PopupCommon.notify("info", "需要调用相关接口:" + this.fileLists)
      this.smjDialogShow = false
    },
    uploadFileHandle(files) {
      this.fileLists = files
    },
    printClick() {
    },
    smjDialogClose() {
      this.smjDialogShow = false
    },
    lmjDialogClose() {
      this.lmjDialogShow = false
    },
    lookSMJRow(d) {
      let newData = []
      if (d.data.checkPict) {
        const tempFileIds = d.data.checkPict.split(",")
        tempFileIds.forEach((item) => {
          if (item) {
            newData.push(Utility.getImgUrl(item))
          }
        })
      }
      this.fileList = newData
      this.lmjDialogShow = true
    },
    submit(d) {
      let self = this
      if (d.id) {
        AjaxCommon("/ccdc/pm/em/filter/emFilterSpecialCheck/edit", "post", d)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                this.$refs.userList.refresh()
                self.initPage()
                self.dialogClose()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
      } else {
        d.examineeCode = self.leftSelectIds[0].examineeCode
        d.examineeEn = self.leftSelectIds[0].examineeEn
        d.examineeName = self.leftSelectIds[0].examineeName
        d.filterNo = self.leftSelectIds[0].filterNo
        d.groupSeq = self.leftSelectIds[0].groupSeq
        d.randomNo = self.leftSelectIds[0].randomNo
        d.drugSeq = self.leftSelectIds[0].drugSeq
        d.sex = self.leftSelectIds[0].sex
        delete d.id
        AjaxCommon("/ccdc/pm/em/filter/emFilterSpecialCheck/add", "post", d)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                this.$refs.userList.refresh()
                self.initPage()
                self.dialogClose()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
      }
    },
    dialogClose() {
      this.dialogShow = false
			this.$refs.userList.handleClearTableRow()
    },
    searchClick() {
      this.pageNo = 1
      this.initPage()
    },
    resetClick() {
      this.examineeName = ""
    },
    addClick() {
      if (this.leftSelectIds.length != 1) {
        PopupCommon.notify("info", "请勾选一个受试者")
        return
      }
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == "fieldName") {
            if (t[res] == "examineeCode") {
              t.filedValue = this.leftSelectIds[0].examineeCode
            } else if (t[res] == "examineeName") {
              t.filedValue = this.leftSelectIds[0].examineeName
            } else if (t[res] == "filterNo") {
              t.filedValue = this.leftSelectIds[0].filterNo
            } else {
              t.filedValue = ""
            }
          }
        })
      })
      formConfigItem.groupId = this.leftSelectIds[0].id
      this.formConfig = formConfigItem

      this.type = "add"
      this.dialogShow = true
      this.dialogTitle =
          "新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    // 分页点击事件
    pageChange(p) {
      this.pageNo = p
      this.initPage()
    },
    editRow(d) {
      this.type = "edit"
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == "fieldName") {
            t.filedValue = d.data[t[res]]
          }
        })
      })
      this.formConfig = formConfigItem
      this.dialogShow = true
      this.dialogTitle =
          "编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    viewRow(d) {
      this.type = "look"
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == "fieldName") {
            t.filedValue = d.data[t[res]]
          }
        })
      })
      this.formConfig = formConfigItem
      this.dialogShow = true
      this.dialogTitle =
          "查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    deleteRow(d) {
      const id = d.data.id
      let self = this
      AjaxCommon(
          "/ccdc/pm/em/filter/emFilterSpecialCheck/delete?id=" + id,
          "delete", {optReason: d.optReason}
      ).then((res) => {
        console.log(res, "res...............")
        if (res.data.code == 200) {
          PopupCommon.notify("success", "删除成功")
          this.$refs.userList.refresh()
          self.initPage()
          self.dialogClose()
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
    // 左侧选中事件
    handleSelectionChange(d) {
      console.log(d)
      this.leftSelectIds = d
      this.examineeCodeList = d ? d.map((t) => t.examineeCode) : []
      this.initPage()
    },
    // 初始化事件
    initPage() {
      let self = this
      AjaxCommon(
          "/ccdc/pm/em/filter/emFilterSpecialCheck/listByPage",
          "post",
          {
            examineeName: this.examineeName,
            examineeCodeList: this.examineeCodeList,
            specialCheckItem: this.specialCheckItem,
            pageNo: this.pageNo,
            pageSize: this.pageSize,
          }
      ).then((res) => {
        if (res.data.code == 200) {
          self.dataSource = res.data.result.records
          self.pageCount = res.data.result.total
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
  },
  mounted() {
    this.initPage()
  },
}
</script>

<style></style>
